#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@File    ：chat_message.py
@Author  ：平
@Date    ：2025/10/2 19:49 
"""

from sqlalchemy import Column, String, Text, DateTime, func
from sqlalchemy.dialects.postgresql import BIGINT

from app.config.config import Base


class ChatMessage(Base):
    __tablename__ = 'chat_message'

    id = Column(BIGINT, primary_key=True, comment='主键id')
    user_id = Column(BIGINT, nullable=False, comment='用户id')
    thread_id = Column(String, nullable=False, comment='会话id')
    title = Column(String(255), comment='会话标题')
    agent = Column(String(255), comment='智能体名称')
    role = Column(String(255), comment='角色(human,assistant)')
    content = Column(Text, comment='消息内容')
    finish_reason = Column(String(255), comment='结束原因')
    created_at = Column(DateTime, default=func.current_timestamp(), comment='创建时间')
    updated_at = Column(DateTime, default=func.current_timestamp(), onupdate=func.current_timestamp(),
                        comment='更新时间')

    def to_dict(self):
        return {
            "id": self.id,
            "userId": self.user_id,
            "threadId": self.thread_id,
            "title": self.title,
            "agent": self.agent,
            "role": self.role,
            "content": self.content,
            "finishReason": self.finish_reason,
            "createdAt": self.created_at,
            "updatedAt": self.updated_at
        }
